﻿using System;
using System.Data;
using System.Globalization;
using ContractManagementSystem.DataAccess;
using ContractManagementSystem.Entity;

[assembly: CLSCompliant(true)]
namespace ContractManagementSystem.BusinessLogic
{
    public static class UserBusiness
    {
        public static string ErrorMessage { get; set; }
        private static UserDataAccess _userDataAccess;

        public static bool ResetPassword(string id)
        {
            _userDataAccess = new UserDataAccess();
            if (_userDataAccess.ResetPassword(id) == 1)
            {
                return true;
            }
            ErrorMessage = _userDataAccess.ErrorMessage;
            return false;
        }

        public static bool CheckContain(string userName)
        {
            _userDataAccess = new UserDataAccess();
            if (_userDataAccess.FetchByExactUserName(userName).Rows.Count>0)
            {
                return true;
            }
            ErrorMessage = _userDataAccess.ErrorMessage;
            return false;
        }

        public static string FetchRoleByUserName(string userName)
        {
            _userDataAccess = new UserDataAccess();
            string result = _userDataAccess.FetchRoleByUserName(userName);
            ErrorMessage = _userDataAccess.ErrorMessage;

            return result;
        }

        public static DataTable FetchByUserName(string userName)
        {
            _userDataAccess = new UserDataAccess();
            return _userDataAccess.FetchByUserName(userName); 
        }

        public static DataTable FetchProfileByUserName(string userName)
        {
            _userDataAccess = new UserDataAccess();
            return _userDataAccess.FetchProfileByUserName(userName); 
        }

        public static string FetchIdByUserName(string userName)
        {
            _userDataAccess = new UserDataAccess();
            if (!string.IsNullOrEmpty(_userDataAccess.ErrorMessage))
            {
                ErrorMessage = _userDataAccess.ErrorMessage;
                return string.Empty;
            }
            string result = _userDataAccess.FetchIdByUserName(userName);
            if (!string.IsNullOrEmpty(_userDataAccess.ErrorMessage))
            {
                ErrorMessage = _userDataAccess.ErrorMessage;
                return string.Empty;
            }
            return result;
        }

        public static DataTable FetchContractorIdByUserName(string userName)
        {
            _userDataAccess = new UserDataAccess();
            return _userDataAccess.FetchContractorIdByUserName(userName);
        }

        public static DataTable FetchByExactUserName(string userName)
        {
            _userDataAccess = new UserDataAccess();
            return _userDataAccess.FetchByExactUserName(userName);
        }

        public static DataTable FetchNameRole()
        {
            _userDataAccess = new UserDataAccess();
            return _userDataAccess.FetchNameRole();
        }

        public static bool UpdateUserByRole(UserEntity userEntity)
        {
            _userDataAccess = new UserDataAccess();
            return _userDataAccess.UpdateUserRole(userEntity);
        }

        public static string FetchPassword(string userName)
        {
            _userDataAccess = new UserDataAccess();
            string result = _userDataAccess.FetchPassword(userName);
            ErrorMessage = _userDataAccess.ErrorMessage;

            return result;
        }

        public static string FetchUserNameById(Guid id)
        {
            _userDataAccess = new UserDataAccess();
            string result = _userDataAccess.FetchUserNameById(id);
            ErrorMessage = _userDataAccess.ErrorMessage;

            return result;
        }

        public static bool UpdatePassword(string password, string name)
        {
            _userDataAccess = new UserDataAccess();
            return _userDataAccess.UpdatePassword(password, name); 
        }

        public static bool CheckLogOn(string userName,string password)
        {
            _userDataAccess = new UserDataAccess();
            if(_userDataAccess.CheckLogOn(userName,password))
            {
                return true;
            }
            ErrorMessage = _userDataAccess.ErrorMessage;
            return false;
        }

        public static DataTable FetchAll(int type)
        {
            return _userDataAccess.FetchAll(type);
        }

        public static bool Delete(string userId)
        {
            _userDataAccess = new UserDataAccess();
            if (_userDataAccess.Delete(userId)==1)
            {
                return true;
            }
            ErrorMessage = _userDataAccess.ErrorMessage;
            return false;
        }

        public static void Insert(string name,string role)
        {
            _userDataAccess = new UserDataAccess();
            InitialUser(name, role);
            ErrorMessage = _userDataAccess.ErrorMessage;
        }

        public static Guid InitialUser(string fullName, string role)
        {
            _userDataAccess = new UserDataAccess();
            string temp = Microsoft.VisualBasic.Strings.StrConv(fullName, Microsoft.VisualBasic.VbStrConv.ProperCase, 0);
            temp = temp.Trim();
            while (Microsoft.VisualBasic.Strings.InStr(temp, "  ", 0) > 0)
            {
                temp = temp.Replace("  ", " ");
            }
            var array = temp.Split(' ');

            var user = array[array.Length - 1];

            for (int i = 0; i < array.Length - 1; i++)
            {
                user += array[i][0];
            }
            var name = user;
            DataTable table = _userDataAccess.FetchByTopUserName(user);

            if(table.Rows.Count>0)
            {
                var result = table.Rows[0][3].ToString();
                var number = result.Replace(user, "");

                if (!string.IsNullOrEmpty(number))
                {
                    int tempNumber = Convert.ToInt32(number,CultureInfo.CurrentCulture);
                    tempNumber++;
                    name += tempNumber;
                }
                else
                {
                    name += "1";
                }
            }

            var entity = new UserEntity
            {
                UserName = name,
                Password = "12345678"
            };

            var roleDataAccess = new RoleDataAccess();
            if (role.Equals("Admin"))
            {
                DataTable id = roleDataAccess.FetchAll("Admin");
                entity.RoleId = new Guid(id.Rows[0][0].ToString());
            }
            else if (role.Equals("Contractor"))
            {
                DataTable id = roleDataAccess.FetchAll("Contractor");
                entity.RoleId = new Guid(id.Rows[0][0].ToString());
            }
            else if (role.Equals("Consultant"))
            {
                DataTable id = roleDataAccess.FetchAll("Consultant");
                entity.RoleId = new Guid(id.Rows[0][0].ToString());
            }
            else
            {
                DataTable id = roleDataAccess.FetchAll("LineManager");
                entity.RoleId = new Guid(id.Rows[0][0].ToString());
            }

            return new Guid(_userDataAccess.Insert(entity));
        }
    }
}
